home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Textfiles / zines / Happle / happle10.sit.hqx / Happle#10 / Files / Denial.sit / DoS / ipfrag.c < prev    next >
C/C++ Source or Header  |  1998-12-09  |  4KB  |  156 lines

  1. /*
  2.  * mtutest.c (C) 1995 Darren Reed <avalon@coombs.anu.edu.au>
  3.  *
  4.  * This was written to test what size TCP fragments would get through
  5.  * various TCP/IP packet filters, as used in IP firewalls.  In certain
  6.  * conditions, enough of the TCP header is missing for unpredictable
  7.  * results unless the filter is aware that this can happen.
  8.  *
  9.  * The author provides this program as-is, with no gaurantee for its
  10.  * suitability for any specific purpose.  The author takes no responsibility
  11.  * for the misuse/abuse of this program and provides it for the sole purpose
  12.  * of testing packet filter policies.  This file maybe distributed freely
  13.  * providing it is not modified and that this notice remains in tact.
  14.  *
  15.  * This was written and tested (successfully) on SunOS 4.1.x.
  16.  * To compiile: cc -Bstatic mtutest.c -o mtutest -lkvm
  17.  */
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <errno.h>
  25. #include <netdb.h>
  26. #include <sys/socket.h>
  27. #include <sys/sockio.h>
  28. #include <net/if.h>
  29. #include <netinet/in.h>
  30. #include <netinet/in_systm.h>
  31. #include <netinet/ip.h>
  32. #include <netinet/tcp.h>
  33. #include <signal.h>
  34. #include <kvm.h>
  35. #include <nlist.h>
  36.  
  37. #define    DESTIP        "127.0.0.1"
  38. #define    DESTPORT    7
  39.  
  40. struct    nlist    nl[2] = {
  41.     { "_ifnet" },
  42.     { NULL }
  43. };
  44.  
  45. char    opts[8];
  46.  
  47. struct ifnet *ifp, ifn;
  48. kvm_t    *k;
  49. int mtu;
  50.  
  51. aclock()
  52. {
  53.     fprintf(stderr, "<alarm>");
  54.     return 0;
  55. }
  56.  
  57. fixmtu()
  58. {
  59.     ifn.if_mtu = mtu;
  60.     kvm_write(k, ifp, &ifn, sizeof(ifn));
  61.     exit(0);
  62. }
  63.  
  64. main(argc, argv)
  65. int argc;
  66. char *argv[];
  67. {
  68.     struct    sockaddr_in    sin, loc;
  69.     struct    protoent    *pe;
  70.     struct    ifaddr    ifa;
  71.     char    ifname[16], buf[32], *lif = "le0";
  72.     int    fd, i, opt;
  73.  
  74.     if (argc > 1)
  75.         lif = argv[1];
  76.  
  77.     k = kvm_open(NULL, NULL, NULL, O_RDWR, "mtutest");
  78.     kvm_nlist(k, nl);
  79.     signal(SIGALRM, aclock);
  80.     if (kvm_read(k, nl[0].n_value, &ifp, sizeof(ifp)) == -1)
  81.         perror("read");
  82.     while (ifp) {
  83.         if (kvm_read(k, ifp, &ifn, sizeof(ifn)) == -1)
  84.             perror("read");
  85.         if (kvm_read(k, ifn.if_name, ifname, sizeof(ifname)) == -1)
  86.             perror("read");
  87.         sprintf(ifname + strlen(ifname), "%d", ifn.if_unit);
  88.         if (!strcmp(ifname, lif))
  89.             break;
  90.         ifp = ifn.if_next;
  91.     }
  92.     if (!ifp) {
  93.         fprintf(stderr, "couldn't find %s\n", lif);
  94.         exit(1);
  95.     }
  96.     kvm_read(k, ifn.if_addrlist, &ifa, sizeof(ifa));
  97.  
  98.     signal(SIGINT, fixmtu);
  99.     signal(SIGQUIT, fixmtu);
  100.     signal(SIGHUP, fixmtu);
  101.     mtu = ifn.if_mtu;
  102.  
  103.     if ((pe = getprotobyname("ip")) == NULL) {
  104.         fprintf(stderr, "ip: unknown protocol\n");
  105.         exit(1);
  106.     }
  107.  
  108.     bcopy((char *)&ifa.ifa_addr, (char *)&loc, sizeof(loc));
  109.     bzero((char *)loc.sin_zero, sizeof(loc.sin_zero));
  110.     loc.sin_family = AF_INET;
  111.     loc.sin_port = 0;
  112.  
  113.     opts[IPOPT_OPTVAL] = IPOPT_NOP;
  114.     opts[IPOPT_OLEN] = 0;
  115.     opts[IPOPT_OFFSET] = 0;
  116.  
  117.     printf("name %s\tip %s\tmetric %d\n", ifname, inet_ntoa(loc.sin_addr),
  118.         ifn.if_mtu);
  119.     printf("minlen: ip %d + tcp %d = %d\n", sizeof(struct ip),
  120.         sizeof(struct tcphdr),
  121.         sizeof(struct ip) + sizeof(struct tcphdr));
  122.  
  123.     for (i = 20; i < 128; i += 2) {
  124.         ifn.if_mtu = i;
  125.         kvm_write(k, ifp, &ifn, sizeof(ifn));
  126.         printf("mtu %d - ", i);
  127.         fflush(stdout);
  128.         fd = socket(AF_INET, SOCK_STREAM, 0);
  129.         opt = 1;
  130.         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt,
  131.                 sizeof(opt)) == -1)
  132.             perror("setsockopt");
  133. /*        if (setsockopt(fd, pe->p_proto, IP_OPTIONS, opts, 4) == -1)
  134.             perror("setsockopt");*/
  135.         loc.sin_port = htons(0);
  136.         bind(fd, &loc, sizeof(loc));
  137.         bzero((char *)&sin, sizeof(sin));
  138.         sin.sin_family = AF_INET;
  139.         sin.sin_port = htons(DESTPORT);
  140.         sin.sin_addr.s_addr = inet_addr(DESTIP);
  141.         alarm(5);
  142.         if (connect(fd, &sin, sizeof(sin)) == -1) {
  143.             if (errno != EINTR)
  144.                 perror("connect");
  145.             else
  146.                 printf("\n");
  147.         } else
  148.             printf("connected\n");
  149.         alarm(0);
  150.         close(fd);
  151.         sleep(1);
  152.     }
  153.     fixmtu();
  154.     /*NOT REACHED*/
  155. }
  156.